package hoge.memoriesalbum.post;

import hoge.memoriesalbum.R;
import hoge.memoriesalbum.util.BitmapUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 * POST対象画像の一覧を表示
 * 
 * @author hyoromo
 */
public class PostSelectActivity extends Activity implements OnClickListener {
    static final int BITMAP_SCALE = 2;
    private static Activity mActivity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_select);
        mActivity = this;

        // ボタン登録
        Button postBtn = (Button) this.findViewById(R.id.post_btn_post);
        postBtn.setOnClickListener(this);

        // 画像
        ImageView img = (ImageView) findViewById(R.id.post_img);
        Intent intent = getIntent();
        String url = intent.getStringExtra("PATH");
        if (url == null) {
            Uri uri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
            img.setImageURI(uri);
        } else {
            int width = getWallpaperDesiredMinimumWidth() / BITMAP_SCALE;
            int height = getWallpaperDesiredMinimumHeight() / BITMAP_SCALE;
            Bitmap bmp = BitmapUtil.scaleBitmap(url, width, height);
            img.setImageBitmap(bmp);
        }

        img.setTag(url);
    }

    @Override
    public void onClick(View v) {
        new PostTask().execute();
    }

    /**
     * GAE側にPOSTする
     */
    private class PostTask extends AsyncTask<Void, Void, Boolean> {
        private ProgressDialog mProgressDialog;

        @Override
        protected void onPreExecute() {
            showPrrogressDialog();
        }

        @Override
        protected final Boolean doInBackground(Void... v) {
            ImageView img = (ImageView) findViewById(R.id.post_img);
            new PostData().post((String) img.getTag());
            return true;
        }

        @Override
        protected final void onPostExecute(Boolean flag) {
            // プログレスダイアログを閉じる
            mProgressDialog.dismiss();
            if (flag) {
                showAlertDialog();
            } else {
                // [TODO]エラーハンドリグ
                finish();
            }
        }

        /**
         * POST中にプログレスダイアログ表示
         */
        private void showPrrogressDialog() {
            String title = getResources().getString(R.string.post_prrogress_dialog_title);
            String mes = getResources().getString(R.string.post_prrogress_dialog_mes);
            mProgressDialog = ProgressDialog.show(mActivity, title, mes, true);
        }

        /**
         * POST後に確認ダイアログ表示
         * 
         * @return
         */
        private void showAlertDialog() {
            new AlertDialog.Builder(mActivity)
            .setTitle(R.string.post_alert_dialog_title)
            .setMessage(R.string.post_alert_dialog_mes)
            .setPositiveButton(R.string.post_alert_dialog_btn,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // 画像選択
                        finish();
                    }
                }).create().show();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        mActivity = null;
    }
}